/*
 * uart.h
 *
 * Created: 7/7/2011 9:55:37 PM
 *  Author: Tim
 */ 

#ifndef UART_H_
#define UART_H_

#include <avr/io.h>
#include <avr/interrupt.h>


#define bufferSizeRx	256

//115200
#define BAUD115200	2158			
#define USART_BAUDCTRLA_115200	BAUD115200
#define USART_BAUDCTRLB_115200	0xA0 | (uint16_t)(0x0FFF & BAUD115200>>8)
#define USART_CTRLB_115200		USART_RXEN_bm | USART_TXEN_bm | USART_CLK2X_bm

//125000
#define BAUD125000	1984		
#define USART_BAUDCTRLA_125000	BAUD125000
#define USART_BAUDCTRLB_125000	0xA0 | (uint16_t)(0x0FFF & BAUD125000>>8)
#define USART_CTRLB_125000		USART_RXEN_bm | USART_TXEN_bm | USART_CLK2X_bm

//230400
#define BAUD230400	2094			
#define USART_BAUDCTRLA_230400	BAUD230400
#define USART_BAUDCTRLB_230400	0x90 | (uint16_t)(0x0FFF & BAUD230400>>8)
#define USART_CTRLB_230400		USART_RXEN_bm | USART_TXEN_bm | USART_CLK2X_bm

typedef struct{
	uint8_t *bufferBegin;
	uint8_t *bufferEnd;
	uint8_t *head;
	uint8_t *tail;
	uint8_t data[bufferSizeRx];
}buf_t;

buf_t bufRxD1;
buf_t bufTxD1;
		

void comPortPut(uint8_t *packet,uint8_t packetLength);

uint8_t comPortGet(uint8_t *packet, uint8_t packetLength);

uint8_t bufferRead(buf_t *buffer);

void bufferWrite(buf_t *buffer, char c);

uint8_t dataInBuf(buf_t *buffer);

void comPortInit();


#endif /* UART_H_ */